package com.cm386.final_project_timezonealert.model;

import java.util.List;

import com.cm386.final_project_timezonealert.R;
import com.cm386.final_project_timezonealert.view.SelectedCityItem;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.SimpleAdapter;

public class UpdateWeatherTask extends AsyncTask<Object, Integer, SimpleAdapter> {

	@Override
	protected SimpleAdapter doInBackground(Object... params) {
		SimpleAdapter adapter     = (SimpleAdapter) params[0];
		List<City> selectedCities = (List<City>) params[1];
		Bitmap defaultWeathIcon = (Bitmap) params[2];
		String defaultTemperature = (String) params[3];
		//Date selectedDateTime = (Date) params[2];
		
		for (int i=0; i<selectedCities.size(); i++){
			City city = selectedCities.get(i);
			SelectedCityItem selectedCityItem = (SelectedCityItem)adapter.getItem(i);
			city.setWeather(WeatherService.getCurrentWeatherByCityName(city.getName(), defaultWeathIcon, defaultTemperature, selectedCityItem));
			//city.setWeather(WeatherService.getWeatherBySelectedDateTimeAndCityName(city.getName(), selectedDateTime)); //It's not very accurate
			selectedCityItem.setWeatherIcon(city.getWeather().getWeatherIcon());
			selectedCityItem.setTemperature(city.getWeather().getTemperature());
			selectedCityItem.setDayNightLabel(city.getWeather().getDayNightLabel());
		}
		return adapter;
	}

	protected void onPostExecute(SimpleAdapter adapter) {
		super.onPostExecute(adapter);
		adapter.notifyDataSetChanged();
	};

}
